<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Unicode Objects and Codecs &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="Concrete Objects Layer" href="concrete.html" />
    <link rel="next" title="Buffer Objects" href="buffer.html" />
    <link rel="prev" title="String/Bytes Objects" href="string.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="buffer.html" title="Buffer Objects"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="string.html" title="String/Bytes Objects"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="unicode-objects-and-codecs">
<span id="unicodeobjects"></span><h1>Unicode Objects and Codecs<a class="headerlink" href="#unicode-objects-and-codecs" title="Permalink to this headline">¶</a></h1>
<div class="section" id="unicode-objects">
<h2>Unicode Objects<a class="headerlink" href="#unicode-objects" title="Permalink to this headline">¶</a></h2>
<p>These are the basic Unicode object types used for the Unicode implementation in
Python:</p>
<dl class="ctype">
<dt id="Py_UNICODE">
<tt class="descname">Py_UNICODE</tt><a class="headerlink" href="#Py_UNICODE" title="Permalink to this definition">¶</a></dt>
<dd>This type represents the storage type which is used by Python internally as
basis for holding Unicode ordinals.  Python&#8217;s default builds use a 16-bit type
for <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> and store Unicode values internally as UCS2. It is also
possible to build a UCS4 version of Python (most recent Linux distributions come
with UCS4 builds of Python). These builds then use a 32-bit type for
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> and store Unicode data internally as UCS4. On platforms
where <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> is available and compatible with the chosen Python
Unicode build variant, <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> is a typedef alias for
<tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> to enhance native platform compatibility. On all other
platforms, <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> is a typedef alias for either <tt class="xref docutils literal"><span class="pre">unsigned</span>
<span class="pre">short</span></tt> (UCS2) or <tt class="xref docutils literal"><span class="pre">unsigned</span> <span class="pre">long</span></tt> (UCS4).</dd></dl>

<p>Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep
this in mind when writing extensions or interfaces.</p>
<dl class="ctype">
<dt id="PyUnicodeObject">
<tt class="descname">PyUnicodeObject</tt><a class="headerlink" href="#PyUnicodeObject" title="Permalink to this definition">¶</a></dt>
<dd>This subtype of <a title="PyObject" class="reference external" href="structures.html#PyObject"><tt class="xref docutils literal"><span class="pre">PyObject</span></tt></a> represents a Python Unicode object.</dd></dl>

<dl class="cvar">
<dt id="PyUnicode_Type">
<a title="PyTypeObject" class="reference external" href="type.html#PyTypeObject">PyTypeObject</a> <tt class="descname">PyUnicode_Type</tt><a class="headerlink" href="#PyUnicode_Type" title="Permalink to this definition">¶</a></dt>
<dd>This instance of <a title="PyTypeObject" class="reference external" href="type.html#PyTypeObject"><tt class="xref docutils literal"><span class="pre">PyTypeObject</span></tt></a> represents the Python Unicode type.  It
is exposed to Python code as <tt class="docutils literal"><span class="pre">unicode</span></tt> and <tt class="docutils literal"><span class="pre">types.UnicodeType</span></tt>.</dd></dl>

<p>The following APIs are really C macros and can be used to do fast checks and to
access internal read-only data of Unicode objects:</p>
<dl class="cfunction">
<dt id="PyUnicode_Check">
int <tt class="descname">PyUnicode_Check</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_Check" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the object <em>o</em> is a Unicode object or an instance of a Unicode
subtype.</p>
<p>
<span class="versionmodified">Changed in version 2.2: </span>Allowed subtypes to be accepted.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_CheckExact">
int <tt class="descname">PyUnicode_CheckExact</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_CheckExact" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the object <em>o</em> is a Unicode object, but not an instance of a
subtype.</p>
<p>
<span class="versionmodified">New in version 2.2.</span></p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_GET_SIZE">
Py_ssize_t <tt class="descname">PyUnicode_GET_SIZE</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_GET_SIZE" title="Permalink to this definition">¶</a></dt>
<dd>Return the size of the object.  <em>o</em> has to be a <a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject"><tt class="xref docutils literal"><span class="pre">PyUnicodeObject</span></tt></a> (not
checked).</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_GET_DATA_SIZE">
Py_ssize_t <tt class="descname">PyUnicode_GET_DATA_SIZE</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_GET_DATA_SIZE" title="Permalink to this definition">¶</a></dt>
<dd>Return the size of the object&#8217;s internal buffer in bytes.  <em>o</em> has to be a
<a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject"><tt class="xref docutils literal"><span class="pre">PyUnicodeObject</span></tt></a> (not checked).</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AS_UNICODE">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a>* <tt class="descname">PyUnicode_AS_UNICODE</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_AS_UNICODE" title="Permalink to this definition">¶</a></dt>
<dd>Return a pointer to the internal <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the object.  <em>o</em>
has to be a <a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject"><tt class="xref docutils literal"><span class="pre">PyUnicodeObject</span></tt></a> (not checked).</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AS_DATA">
const char* <tt class="descname">PyUnicode_AS_DATA</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *o</em><big>)</big><a class="headerlink" href="#PyUnicode_AS_DATA" title="Permalink to this definition">¶</a></dt>
<dd>Return a pointer to the internal buffer of the object. <em>o</em> has to be a
<a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject"><tt class="xref docutils literal"><span class="pre">PyUnicodeObject</span></tt></a> (not checked).</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_ClearFreeList">
int <tt class="descname">PyUnicode_ClearFreeList</tt><big>(</big>void<big>)</big><a class="headerlink" href="#PyUnicode_ClearFreeList" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear the free list. Return the total number of freed items.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<p>Unicode provides many different character properties. The most often needed ones
are available through these macros which are mapped to C functions depending on
the Python configuration.</p>
<dl class="cfunction">
<dt id="Py_UNICODE_ISSPACE">
int <tt class="descname">Py_UNICODE_ISSPACE</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISSPACE" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a whitespace character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISLOWER">
int <tt class="descname">Py_UNICODE_ISLOWER</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISLOWER" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a lowercase character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISUPPER">
int <tt class="descname">Py_UNICODE_ISUPPER</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISUPPER" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is an uppercase character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISTITLE">
int <tt class="descname">Py_UNICODE_ISTITLE</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISTITLE" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a titlecase character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISLINEBREAK">
int <tt class="descname">Py_UNICODE_ISLINEBREAK</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISLINEBREAK" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a linebreak character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISDECIMAL">
int <tt class="descname">Py_UNICODE_ISDECIMAL</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISDECIMAL" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a decimal character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISDIGIT">
int <tt class="descname">Py_UNICODE_ISDIGIT</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISDIGIT" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a digit character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISNUMERIC">
int <tt class="descname">Py_UNICODE_ISNUMERIC</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISNUMERIC" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is a numeric character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISALPHA">
int <tt class="descname">Py_UNICODE_ISALPHA</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISALPHA" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is an alphabetic character.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_ISALNUM">
int <tt class="descname">Py_UNICODE_ISALNUM</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_ISALNUM" title="Permalink to this definition">¶</a></dt>
<dd>Return 1 or 0 depending on whether <em>ch</em> is an alphanumeric character.</dd></dl>

<p>These APIs can be used for fast direct character conversions:</p>
<dl class="cfunction">
<dt id="Py_UNICODE_TOLOWER">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a> <tt class="descname">Py_UNICODE_TOLOWER</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TOLOWER" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to lower case.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TOUPPER">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a> <tt class="descname">Py_UNICODE_TOUPPER</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TOUPPER" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to upper case.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TOTITLE">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a> <tt class="descname">Py_UNICODE_TOTITLE</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TOTITLE" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to title case.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TODECIMAL">
int <tt class="descname">Py_UNICODE_TODECIMAL</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TODECIMAL" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to a decimal positive integer.  Return
<tt class="docutils literal"><span class="pre">-1</span></tt> if this is not possible.  This macro does not raise exceptions.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TODIGIT">
int <tt class="descname">Py_UNICODE_TODIGIT</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TODIGIT" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to a single digit integer. Return <tt class="docutils literal"><span class="pre">-1</span></tt> if
this is not possible.  This macro does not raise exceptions.</dd></dl>

<dl class="cfunction">
<dt id="Py_UNICODE_TONUMERIC">
double <tt class="descname">Py_UNICODE_TONUMERIC</tt><big>(</big><a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> ch</em><big>)</big><a class="headerlink" href="#Py_UNICODE_TONUMERIC" title="Permalink to this definition">¶</a></dt>
<dd>Return the character <em>ch</em> converted to a double. Return <tt class="docutils literal"><span class="pre">-1.0</span></tt> if this is not
possible.  This macro does not raise exceptions.</dd></dl>

<p>To create Unicode objects and access their basic sequence properties, use these
APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_FromUnicode">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromUnicode</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *u</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyUnicode_FromUnicode" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode Object from the Py_UNICODE buffer <em>u</em> of the given size. <em>u</em>
may be <em>NULL</em> which causes the contents to be undefined. It is the user&#8217;s
responsibility to fill in the needed data.  The buffer is copied into the new
object. If the buffer is not <em>NULL</em>, the return value might be a shared object.
Therefore, modification of the resulting Unicode object is only allowed when <em>u</em>
is <em>NULL</em>.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUnicode">
<a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a>* <tt class="descname">PyUnicode_AsUnicode</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUnicode" title="Permalink to this definition">¶</a></dt>
<dd>Return a read-only pointer to the Unicode object&#8217;s internal <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a>
buffer, <em>NULL</em> if <em>unicode</em> is not a Unicode object.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_GetSize">
Py_ssize_t <tt class="descname">PyUnicode_GetSize</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_GetSize" title="Permalink to this definition">¶</a></dt>
<dd>Return the length of the Unicode object.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_FromEncodedObject">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromEncodedObject</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *obj</em>, const char<em> *encoding</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_FromEncodedObject" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Coerce an encoded object <em>obj</em> to an Unicode object and return a reference with
incremented refcount.</p>
<p>String and other char buffer compatible objects are decoded according to the
given encoding and using the error handling defined by errors.  Both can be
<em>NULL</em> to have the interface use the default values (see the next section for
details).</p>
<p>All other objects, including Unicode objects, cause a <a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> to be
set.</p>
<p>The API returns <em>NULL</em> if there was an error.  The caller is responsible for
decref&#8217;ing the returned objects.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_FromObject">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromObject</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *obj</em><big>)</big><a class="headerlink" href="#PyUnicode_FromObject" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Shortcut for <tt class="docutils literal"><span class="pre">PyUnicode_FromEncodedObject(obj,</span> <span class="pre">NULL,</span> <span class="pre">&quot;strict&quot;)</span></tt> which is used
throughout the interpreter whenever coercion to Unicode is needed.</p>
</dd></dl>

<p>If the platform supports <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> and provides a header file wchar.h,
Python can interface directly to this type using the following functions.
Support is optimized if Python&#8217;s own <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> type is identical to
the system&#8217;s <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt>.</p>
<dl class="cfunction">
<dt id="PyUnicode_FromWideChar">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_FromWideChar</tt><big>(</big>const wchar_t<em> *w</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyUnicode_FromWideChar" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object from the <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> buffer <em>w</em> of the given size.
Return <em>NULL</em> on failure.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsWideChar">
Py_ssize_t <tt class="descname">PyUnicode_AsWideChar</tt><big>(</big><a title="PyUnicodeObject" class="reference internal" href="#PyUnicodeObject">PyUnicodeObject</a><em> *unicode</em>, wchar_t<em> *w</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyUnicode_AsWideChar" title="Permalink to this definition">¶</a></dt>
<dd>Copy the Unicode object contents into the <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> buffer <em>w</em>.  At most
<em>size</em> <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> characters are copied (excluding a possibly trailing
0-termination character).  Return the number of <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> characters
copied or -1 in case of an error.  Note that the resulting <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt>
string may or may not be 0-terminated.  It is the responsibility of the caller
to make sure that the <tt class="xref docutils literal"><span class="pre">wchar_t</span></tt> string is 0-terminated in case this is
required by the application.</dd></dl>

</div>
<div class="section" id="built-in-codecs">
<span id="builtincodecs"></span><h2>Built-in Codecs<a class="headerlink" href="#built-in-codecs" title="Permalink to this headline">¶</a></h2>
<p>Python provides a set of builtin codecs which are written in C for speed. All of
these codecs are directly usable via the following functions.</p>
<p>Many of the following APIs take two arguments encoding and errors. These
parameters encoding and errors have the same semantics as the ones of the
builtin unicode() Unicode object constructor.</p>
<p>Setting encoding to <em>NULL</em> causes the default encoding to be used which is
ASCII.  The file system calls should use <tt class="xref docutils literal"><span class="pre">Py_FileSystemDefaultEncoding</span></tt>
as the encoding for file names. This variable should be treated as read-only: On
some systems, it will be a pointer to a static string, on others, it will change
at run-time (such as when the application invokes setlocale).</p>
<p>Error handling is set by errors which may also be set to <em>NULL</em> meaning to use
the default handling defined for the codec.  Default error handling for all
builtin codecs is &#8220;strict&#8221; (<a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> is raised).</p>
<p>The codecs all use a similar interface.  Only deviation from the following
generic ones are documented for simplicity.</p>
<p>These are the generic codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_Decode">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Decode</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *encoding</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_Decode" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the encoded string <em>s</em>.
<em>encoding</em> and <em>errors</em> have the same meaning as the parameters of the same name
in the <a title="unicode" class="reference external" href="../library/functions.html#unicode"><tt class="xref docutils literal"><span class="pre">unicode()</span></tt></a> builtin function.  The codec to be used is looked up
using the Python codec registry.  Return <em>NULL</em> if an exception was raised by
the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Encode">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Encode</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *encoding</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_Encode" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size and return a Python
string object.  <em>encoding</em> and <em>errors</em> have the same meaning as the parameters
of the same name in the Unicode <tt class="xref docutils literal"><span class="pre">encode()</span></tt> method.  The codec to be used is
looked up using the Python codec registry.  Return <em>NULL</em> if an exception was
raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsEncodedString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsEncodedString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em>, const char<em> *encoding</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_AsEncodedString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object and return the result as Python string object.
<em>encoding</em> and <em>errors</em> have the same meaning as the parameters of the same name
in the Unicode <tt class="xref docutils literal"><span class="pre">encode()</span></tt> method. The codec to be used is looked up using
the Python codec registry. Return <em>NULL</em> if an exception was raised by the
codec.</p>
</dd></dl>

<p>These are the UTF-8 codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF8">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF8</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF8" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the UTF-8 encoded string
<em>s</em>. Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF8Stateful">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF8Stateful</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, Py_ssize_t<em> *consumed</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF8Stateful" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>If <em>consumed</em> is <em>NULL</em>, behave like <a title="PyUnicode_DecodeUTF8" class="reference internal" href="#PyUnicode_DecodeUTF8"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF8</span></tt></a>. If
<em>consumed</em> is not <em>NULL</em>, trailing incomplete UTF-8 byte sequences will not be
treated as an error. Those bytes will not be decoded and the number of bytes
that have been decoded will be stored in <em>consumed</em>.</p>
<p>
<span class="versionmodified">New in version 2.4.</span></p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeUTF8">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeUTF8</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeUTF8" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using UTF-8 and return a
Python string object.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUTF8String">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsUTF8String</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUTF8String" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using UTF-8 and return the result as Python string
object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an exception was raised
by the codec.</p>
</dd></dl>

<p>These are the UTF-32 codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF32">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF32</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF32" title="Permalink to this definition">¶</a></dt>
<dd><p>Decode <em>length</em> bytes from a UTF-32 encoded buffer string and return the
corresponding Unicode object.  <em>errors</em> (if non-<em>NULL</em>) defines the error
handling. It defaults to &#8220;strict&#8221;.</p>
<p>If <em>byteorder</em> is non-<em>NULL</em>, the decoder starts decoding using the given byte
order:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">:</span> <span class="n">little</span> <span class="n">endian</span>
<span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="mi">0</span><span class="o">:</span>  <span class="n">native</span> <span class="n">order</span>
<span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="mi">1</span><span class="o">:</span>  <span class="n">big</span> <span class="n">endian</span>
</pre></div>
</div>
<p>and then switches if the first four bytes of the input data are a byte order mark
(BOM) and the specified byte order is native order.  This BOM is not copied into
the resulting Unicode string.  After completion, <em>*byteorder</em> is set to the
current byte order at the end of input data.</p>
<p>In a narrow build codepoints outside the BMP will be decoded as surrogate pairs.</p>
<p>If <em>byteorder</em> is <em>NULL</em>, the codec starts in native order mode.</p>
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF32Stateful">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF32Stateful</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em>, Py_ssize_t<em> *consumed</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF32Stateful" title="Permalink to this definition">¶</a></dt>
<dd><p>If <em>consumed</em> is <em>NULL</em>, behave like <a title="PyUnicode_DecodeUTF32" class="reference internal" href="#PyUnicode_DecodeUTF32"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF32</span></tt></a>. If
<em>consumed</em> is not <em>NULL</em>, <a title="PyUnicode_DecodeUTF32Stateful" class="reference internal" href="#PyUnicode_DecodeUTF32Stateful"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF32Stateful</span></tt></a> will not treat
trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible
by four) as an error. Those bytes will not be decoded and the number of bytes
that have been decoded will be stored in <em>consumed</em>.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeUTF32">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeUTF32</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> byteorder</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeUTF32" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a Python bytes object holding the UTF-32 encoded value of the Unicode
data in <em>s</em>.  If <em>byteorder</em> is not <tt class="docutils literal"><span class="pre">0</span></tt>, output is written according to the
following byte order:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="n">byteorder</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">:</span> <span class="n">little</span> <span class="n">endian</span>
<span class="n">byteorder</span> <span class="o">==</span> <span class="mi">0</span><span class="o">:</span>  <span class="n">native</span> <span class="n">byte</span> <span class="n">order</span> <span class="p">(</span><span class="n">writes</span> <span class="n">a</span> <span class="n">BOM</span> <span class="n">mark</span><span class="p">)</span>
<span class="n">byteorder</span> <span class="o">==</span> <span class="mi">1</span><span class="o">:</span>  <span class="n">big</span> <span class="n">endian</span>
</pre></div>
</div>
<p>If byteorder is <tt class="docutils literal"><span class="pre">0</span></tt>, the output string will always start with the Unicode BOM
mark (U+FEFF). In the other two modes, no BOM mark is prepended.</p>
<p>If <em>Py_UNICODE_WIDE</em> is not defined, surrogate pairs will be output
as a single codepoint.</p>
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUTF32String">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsUTF32String</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUTF32String" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a Python string using the UTF-32 encoding in native byte order. The
string always starts with a BOM mark.  Error handling is &#8220;strict&#8221;.  Return
<em>NULL</em> if an exception was raised by the codec.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<p>These are the UTF-16 codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF16">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF16</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF16" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Decode <em>length</em> bytes from a UTF-16 encoded buffer string and return the
corresponding Unicode object.  <em>errors</em> (if non-<em>NULL</em>) defines the error
handling. It defaults to &#8220;strict&#8221;.</p>
<p>If <em>byteorder</em> is non-<em>NULL</em>, the decoder starts decoding using the given byte
order:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">:</span> <span class="n">little</span> <span class="n">endian</span>
<span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="mi">0</span><span class="o">:</span>  <span class="n">native</span> <span class="n">order</span>
<span class="o">*</span><span class="n">byteorder</span> <span class="o">==</span> <span class="mi">1</span><span class="o">:</span>  <span class="n">big</span> <span class="n">endian</span>
</pre></div>
</div>
<p>and then switches if the first two bytes of the input data are a byte order mark
(BOM) and the specified byte order is native order.  This BOM is not copied into
the resulting Unicode string.  After completion, <em>*byteorder</em> is set to the
current byte order at the.</p>
<p>If <em>byteorder</em> is <em>NULL</em>, the codec starts in native order mode.</p>
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_DecodeUTF16Stateful">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUTF16Stateful</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> *byteorder</em>, Py_ssize_t<em> *consumed</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUTF16Stateful" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>If <em>consumed</em> is <em>NULL</em>, behave like <a title="PyUnicode_DecodeUTF16" class="reference internal" href="#PyUnicode_DecodeUTF16"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF16</span></tt></a>. If
<em>consumed</em> is not <em>NULL</em>, <a title="PyUnicode_DecodeUTF16Stateful" class="reference internal" href="#PyUnicode_DecodeUTF16Stateful"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeUTF16Stateful</span></tt></a> will not treat
trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a
split surrogate pair) as an error. Those bytes will not be decoded and the
number of bytes that have been decoded will be stored in <em>consumed</em>.</p>
<p>
<span class="versionmodified">New in version 2.4.</span></p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeUTF16">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeUTF16</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em>, int<em> byteorder</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeUTF16" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a Python string object holding the UTF-16 encoded value of the Unicode
data in <em>s</em>.  If <em>byteorder</em> is not <tt class="docutils literal"><span class="pre">0</span></tt>, output is written according to the
following byte order:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="n">byteorder</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">:</span> <span class="n">little</span> <span class="n">endian</span>
<span class="n">byteorder</span> <span class="o">==</span> <span class="mi">0</span><span class="o">:</span>  <span class="n">native</span> <span class="n">byte</span> <span class="n">order</span> <span class="p">(</span><span class="n">writes</span> <span class="n">a</span> <span class="n">BOM</span> <span class="n">mark</span><span class="p">)</span>
<span class="n">byteorder</span> <span class="o">==</span> <span class="mi">1</span><span class="o">:</span>  <span class="n">big</span> <span class="n">endian</span>
</pre></div>
</div>
<p>If byteorder is <tt class="docutils literal"><span class="pre">0</span></tt>, the output string will always start with the Unicode BOM
mark (U+FEFF). In the other two modes, no BOM mark is prepended.</p>
<p>If <em>Py_UNICODE_WIDE</em> is defined, a single <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> value may get
represented as a surrogate pair. If it is not defined, each <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a>
values is interpreted as an UCS-2 character.</p>
<p>Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUTF16String">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsUTF16String</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUTF16String" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a Python string using the UTF-16 encoding in native byte order. The
string always starts with a BOM mark.  Error handling is &#8220;strict&#8221;.  Return
<em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<p>These are the &#8220;Unicode Escape&#8221; codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeUnicodeEscape">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeUnicodeEscape</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeUnicodeEscape" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the Unicode-Escape encoded
string <em>s</em>.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeUnicodeEscape">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeUnicodeEscape</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeUnicodeEscape" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using Unicode-Escape and
return a Python string object.  Return <em>NULL</em> if an exception was raised by the
codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsUnicodeEscapeString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsUnicodeEscapeString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsUnicodeEscapeString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using Unicode-Escape and return the result as Python
string object.  Error handling is &#8220;strict&#8221;. Return <em>NULL</em> if an exception was
raised by the codec.</p>
</dd></dl>

<p>These are the &#8220;Raw Unicode Escape&#8221; codec APIs:</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeRawUnicodeEscape">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeRawUnicodeEscape</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeRawUnicodeEscape" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the Raw-Unicode-Escape
encoded string <em>s</em>.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeRawUnicodeEscape">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeRawUnicodeEscape</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeRawUnicodeEscape" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using Raw-Unicode-Escape
and return a Python string object.  Return <em>NULL</em> if an exception was raised by
the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsRawUnicodeEscapeString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsRawUnicodeEscapeString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsRawUnicodeEscapeString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using Raw-Unicode-Escape and return the result as
Python string object. Error handling is &#8220;strict&#8221;. Return <em>NULL</em> if an exception
was raised by the codec.</p>
</dd></dl>

<p>These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode
ordinals and only these are accepted by the codecs during encoding.</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeLatin1">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeLatin1</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeLatin1" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the Latin-1 encoded string
<em>s</em>.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeLatin1">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeLatin1</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeLatin1" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using Latin-1 and return
a Python string object.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsLatin1String">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsLatin1String</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsLatin1String" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using Latin-1 and return the result as Python string
object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an exception was raised
by the codec.</p>
</dd></dl>

<p>These are the ASCII codec APIs.  Only 7-bit ASCII data is accepted. All other
codes generate errors.</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeASCII">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeASCII</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeASCII" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the ASCII encoded string
<em>s</em>.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeASCII">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeASCII</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeASCII" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using ASCII and return a
Python string object.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsASCIIString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsASCIIString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsASCIIString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using ASCII and return the result as Python string
object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an exception was raised
by the codec.</p>
</dd></dl>

<p>These are the mapping codec APIs:</p>
<p>This codec is special in that it can be used to implement many different codecs
(and this is in fact what was done to obtain most of the standard codecs
included in the <tt class="xref docutils literal"><span class="pre">encodings</span></tt> package). The codec uses mapping to encode and
decode characters.</p>
<p>Decoding mappings must map single string characters to single Unicode
characters, integers (which are then interpreted as Unicode ordinals) or None
(meaning &#8220;undefined mapping&#8221; and causing an error).</p>
<p>Encoding mappings must map single Unicode characters to single string
characters, integers (which are then interpreted as Latin-1 ordinals) or None
(meaning &#8220;undefined mapping&#8221; and causing an error).</p>
<p>The mapping objects provided must only support the __getitem__ mapping
interface.</p>
<p>If a character lookup fails with a LookupError, the character is copied as-is
meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal
resp. Because of this, mappings only need to contain those mappings which map
characters to different code points.</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeCharmap">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeCharmap</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *mapping</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeCharmap" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the encoded string <em>s</em> using
the given <em>mapping</em> object.  Return <em>NULL</em> if an exception was raised by the
codec. If <em>mapping</em> is <em>NULL</em> latin-1 decoding will be done. Else it can be a
dictionary mapping byte or a unicode string, which is treated as a lookup table.
Byte values greater that the length of the string and U+FFFE &#8220;characters&#8221; are
treated as &#8220;undefined mapping&#8221;.</p>
<p>
<span class="versionmodified">Changed in version 2.4: </span>Allowed unicode string as mapping argument.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeCharmap">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeCharmap</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *mapping</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeCharmap" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using the given
<em>mapping</em> object and return a Python string object. Return <em>NULL</em> if an
exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsCharmapString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsCharmapString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *mapping</em><big>)</big><a class="headerlink" href="#PyUnicode_AsCharmapString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using the given <em>mapping</em> object and return the result
as Python string object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an
exception was raised by the codec.</p>
</dd></dl>

<p>The following codec API is special in that maps Unicode to Unicode.</p>
<dl class="cfunction">
<dt id="PyUnicode_TranslateCharmap">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_TranslateCharmap</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *table</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_TranslateCharmap" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Translate a <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given length by applying a
character mapping <em>table</em> to it and return the resulting Unicode object.  Return
<em>NULL</em> when an exception was raised by the codec.</p>
<p>The <em>mapping</em> table must map Unicode ordinal integers to Unicode ordinal
integers or None (causing deletion of the character).</p>
<p>Mapping tables need only provide the <a title="object.__getitem__" class="reference external" href="../reference/datamodel.html#object.__getitem__"><tt class="xref docutils literal"><span class="pre">__getitem__()</span></tt></a> interface; dictionaries
and sequences work well.  Unmapped character ordinals (ones which cause a
<a title="exceptions.LookupError" class="reference external" href="../library/exceptions.html#exceptions.LookupError"><tt class="xref docutils literal"><span class="pre">LookupError</span></tt></a>) are left untouched and are copied as-is.</p>
</dd></dl>

<p>These are the MBCS codec APIs. They are currently only available on Windows and
use the Win32 MBCS converters to implement the conversions.  Note that MBCS (or
DBCS) is a class of encodings, not just one.  The target encoding is defined by
the user settings on the machine running the codec.</p>
<dl class="cfunction">
<dt id="PyUnicode_DecodeMBCS">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeMBCS</tt><big>(</big>const char<em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeMBCS" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a Unicode object by decoding <em>size</em> bytes of the MBCS encoded string <em>s</em>.
Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_DecodeMBCSStateful">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_DecodeMBCSStateful</tt><big>(</big>const char<em> *s</em>, int<em> size</em>, const char<em> *errors</em>, int<em> *consumed</em><big>)</big><a class="headerlink" href="#PyUnicode_DecodeMBCSStateful" title="Permalink to this definition">¶</a></dt>
<dd><p>If <em>consumed</em> is <em>NULL</em>, behave like <a title="PyUnicode_DecodeMBCS" class="reference internal" href="#PyUnicode_DecodeMBCS"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeMBCS</span></tt></a>. If
<em>consumed</em> is not <em>NULL</em>, <a title="PyUnicode_DecodeMBCSStateful" class="reference internal" href="#PyUnicode_DecodeMBCSStateful"><tt class="xref docutils literal"><span class="pre">PyUnicode_DecodeMBCSStateful</span></tt></a> will not decode
trailing lead byte and the number of bytes that have been decoded will be stored
in <em>consumed</em>.</p>
<p>
<span class="versionmodified">New in version 2.5.</span></p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_EncodeMBCS">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_EncodeMBCS</tt><big>(</big>const <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE">Py_UNICODE</a><em> *s</em>, Py_ssize_t<em> size</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_EncodeMBCS" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode the <a title="Py_UNICODE" class="reference internal" href="#Py_UNICODE"><tt class="xref docutils literal"><span class="pre">Py_UNICODE</span></tt></a> buffer of the given size using MBCS and return a
Python string object.  Return <em>NULL</em> if an exception was raised by the codec.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_AsMBCSString">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_AsMBCSString</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *unicode</em><big>)</big><a class="headerlink" href="#PyUnicode_AsMBCSString" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Encode a Unicode object using MBCS and return the result as Python string
object.  Error handling is &#8220;strict&#8221;.  Return <em>NULL</em> if an exception was raised
by the codec.</p>
</dd></dl>

</div>
<div class="section" id="methods-and-slot-functions">
<span id="unicodemethodsandslots"></span><h2>Methods and Slot Functions<a class="headerlink" href="#methods-and-slot-functions" title="Permalink to this headline">¶</a></h2>
<p>The following APIs are capable of handling Unicode objects and strings on input
(we refer to them as strings in the descriptions) and return Unicode objects or
integers as appropriate.</p>
<p>They all return <em>NULL</em> or <tt class="docutils literal"><span class="pre">-1</span></tt> if an exception occurs.</p>
<dl class="cfunction">
<dt id="PyUnicode_Concat">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Concat</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *left</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *right</em><big>)</big><a class="headerlink" href="#PyUnicode_Concat" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Concat two strings giving a new Unicode string.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Split">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Split</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *s</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *sep</em>, Py_ssize_t<em> maxsplit</em><big>)</big><a class="headerlink" href="#PyUnicode_Split" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Split a string giving a list of Unicode strings.  If sep is <em>NULL</em>, splitting
will be done at all whitespace substrings.  Otherwise, splits occur at the given
separator.  At most <em>maxsplit</em> splits will be done.  If negative, no limit is
set.  Separators are not included in the resulting list.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Splitlines">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Splitlines</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *s</em>, int<em> keepend</em><big>)</big><a class="headerlink" href="#PyUnicode_Splitlines" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Split a Unicode string at line breaks, returning a list of Unicode strings.
CRLF is considered to be one line break.  If <em>keepend</em> is 0, the Line break
characters are not included in the resulting strings.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Translate">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Translate</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *table</em>, const char<em> *errors</em><big>)</big><a class="headerlink" href="#PyUnicode_Translate" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Translate a string by applying a character mapping table to it and return the
resulting Unicode object.</p>
<p>The mapping table must map Unicode ordinal integers to Unicode ordinal integers
or None (causing deletion of the character).</p>
<p>Mapping tables need only provide the <a title="object.__getitem__" class="reference external" href="../reference/datamodel.html#object.__getitem__"><tt class="xref docutils literal"><span class="pre">__getitem__()</span></tt></a> interface; dictionaries
and sequences work well.  Unmapped character ordinals (ones which cause a
<a title="exceptions.LookupError" class="reference external" href="../library/exceptions.html#exceptions.LookupError"><tt class="xref docutils literal"><span class="pre">LookupError</span></tt></a>) are left untouched and are copied as-is.</p>
<p><em>errors</em> has the usual meaning for codecs. It may be <em>NULL</em> which indicates to
use the default error handling.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Join">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Join</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *separator</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *seq</em><big>)</big><a class="headerlink" href="#PyUnicode_Join" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Join a sequence of strings using the given separator and return the resulting
Unicode string.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Tailmatch">
int <tt class="descname">PyUnicode_Tailmatch</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *substr</em>, Py_ssize_t<em> start</em>, Py_ssize_t<em> end</em>, int<em> direction</em><big>)</big><a class="headerlink" href="#PyUnicode_Tailmatch" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return 1 if <em>substr</em> matches <em>str*[*start</em>:<em>end</em>] at the given tail end
(<em>direction</em> == -1 means to do a prefix match, <em>direction</em> == 1 a suffix match),
0 otherwise. Return <tt class="docutils literal"><span class="pre">-1</span></tt> if an error occurred.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Find">
Py_ssize_t <tt class="descname">PyUnicode_Find</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *substr</em>, Py_ssize_t<em> start</em>, Py_ssize_t<em> end</em>, int<em> direction</em><big>)</big><a class="headerlink" href="#PyUnicode_Find" title="Permalink to this definition">¶</a></dt>
<dd>Return the first position of <em>substr</em> in <em>str*[*start</em>:<em>end</em>] using the given
<em>direction</em> (<em>direction</em> == 1 means to do a forward search, <em>direction</em> == -1 a
backward search).  The return value is the index of the first match; a value of
<tt class="docutils literal"><span class="pre">-1</span></tt> indicates that no match was found, and <tt class="docutils literal"><span class="pre">-2</span></tt> indicates that an error
occurred and an exception has been set.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Count">
Py_ssize_t <tt class="descname">PyUnicode_Count</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *substr</em>, Py_ssize_t<em> start</em>, Py_ssize_t<em> end</em><big>)</big><a class="headerlink" href="#PyUnicode_Count" title="Permalink to this definition">¶</a></dt>
<dd>Return the number of non-overlapping occurrences of <em>substr</em> in
<tt class="docutils literal"><span class="pre">str[start:end]</span></tt>.  Return <tt class="docutils literal"><span class="pre">-1</span></tt> if an error occurred.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Replace">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Replace</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *str</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *substr</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *replstr</em>, Py_ssize_t<em> maxcount</em><big>)</big><a class="headerlink" href="#PyUnicode_Replace" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Replace at most <em>maxcount</em> occurrences of <em>substr</em> in <em>str</em> with <em>replstr</em> and
return the resulting Unicode object. <em>maxcount</em> == -1 means replace all
occurrences.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Compare">
int <tt class="descname">PyUnicode_Compare</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *left</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *right</em><big>)</big><a class="headerlink" href="#PyUnicode_Compare" title="Permalink to this definition">¶</a></dt>
<dd>Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
respectively.</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_RichCompare">
int <tt class="descname">PyUnicode_RichCompare</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *left</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *right</em>, int<em> op</em><big>)</big><a class="headerlink" href="#PyUnicode_RichCompare" title="Permalink to this definition">¶</a></dt>
<dd><p>Rich compare two unicode strings and return one of the following:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">NULL</span></tt> in case an exception was raised</li>
<li><a title="Py_True" class="reference external" href="bool.html#Py_True"><tt class="xref docutils literal"><span class="pre">Py_True</span></tt></a> or <a title="Py_False" class="reference external" href="bool.html#Py_False"><tt class="xref docutils literal"><span class="pre">Py_False</span></tt></a> for successful comparisons</li>
<li><tt class="xref docutils literal"><span class="pre">Py_NotImplemented</span></tt> in case the type combination is unknown</li>
</ul>
<p>Note that <tt class="xref docutils literal"><span class="pre">Py_EQ</span></tt> and <tt class="xref docutils literal"><span class="pre">Py_NE</span></tt> comparisons can cause a
<a title="exceptions.UnicodeWarning" class="reference external" href="../library/exceptions.html#exceptions.UnicodeWarning"><tt class="xref docutils literal"><span class="pre">UnicodeWarning</span></tt></a> in case the conversion of the arguments to Unicode fails
with a <a title="exceptions.UnicodeDecodeError" class="reference external" href="../library/exceptions.html#exceptions.UnicodeDecodeError"><tt class="xref docutils literal"><span class="pre">UnicodeDecodeError</span></tt></a>.</p>
<p>Possible values for <em>op</em> are <tt class="xref docutils literal"><span class="pre">Py_GT</span></tt>, <tt class="xref docutils literal"><span class="pre">Py_GE</span></tt>, <tt class="xref docutils literal"><span class="pre">Py_EQ</span></tt>,
<tt class="xref docutils literal"><span class="pre">Py_NE</span></tt>, <tt class="xref docutils literal"><span class="pre">Py_LT</span></tt>, and <tt class="xref docutils literal"><span class="pre">Py_LE</span></tt>.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Format">
<a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a>* <tt class="descname">PyUnicode_Format</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *format</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *args</em><big>)</big><a class="headerlink" href="#PyUnicode_Format" title="Permalink to this definition">¶</a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Return a new string object from <em>format</em> and <em>args</em>; this is analogous to
<tt class="docutils literal"><span class="pre">format</span> <span class="pre">%</span> <span class="pre">args</span></tt>.  The <em>args</em> argument must be a tuple.</p>
</dd></dl>

<dl class="cfunction">
<dt id="PyUnicode_Contains">
int <tt class="descname">PyUnicode_Contains</tt><big>(</big><a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *container</em>, <a title="PyObject" class="reference external" href="structures.html#PyObject">PyObject</a><em> *element</em><big>)</big><a class="headerlink" href="#PyUnicode_Contains" title="Permalink to this definition">¶</a></dt>
<dd><p>Check whether <em>element</em> is contained in <em>container</em> and return true or false
accordingly.</p>
<p><em>element</em> has to coerce to a one element Unicode string. <tt class="docutils literal"><span class="pre">-1</span></tt> is returned if
there was an error.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">Unicode Objects and Codecs</a><ul>
<li><a class="reference external" href="#unicode-objects">Unicode Objects</a></li>
<li><a class="reference external" href="#built-in-codecs">Built-in Codecs</a></li>
<li><a class="reference external" href="#methods-and-slot-functions">Methods and Slot Functions</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="string.html"
                                  title="previous chapter">String/Bytes Objects</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="buffer.html"
                                  title="next chapter">Buffer Objects</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/c-api/unicode.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="buffer.html" title="Buffer Objects"
             >next</a> |</li>
        <li class="right" >
          <a href="string.html" title="String/Bytes Objects"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="concrete.html" >Concrete Objects Layer</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>